package com.sky.service.admin.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sky.mapper.admin.AddressBookMapper;
import com.sky.pojo.AddressBook;
import com.sky.result.Result;
import com.sky.service.admin.AddressBookService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

@Service
@Slf4j
@RequiredArgsConstructor
public class AddressBookServiceImpl implements AddressBookService {
    private final AddressBookMapper addressBookMapper;

    /*
     * 新增地址
     * */
    @Override
    public void saveAddressBook(AddressBook addressBook) {
     addressBookMapper.insert(addressBook);
    }

    /*
     * 查询登录用户所有的地址
     * */
    @Override
    public List<AddressBook> selectAddressBoorList() {
        List<AddressBook> addressBookList = addressBookMapper.selectList(Wrappers.<AddressBook>lambdaQuery());
        return addressBookList;
    }

    /*
     * 查询默认地址
     * */
    @Override
    public AddressBook selectAddressBookDefault() {
        AddressBook addressBook = addressBookMapper.selectOne(Wrappers.<AddressBook>lambdaQuery().eq(AddressBook::getIsDefault, AddressBook.IS_DEFAULT_YES));
        return addressBook;
    }

    /*
     * 修改地址
     * */
    @Override
    public void updateAddressBook(AddressBook addressBook) {
        addressBookMapper.update(addressBook,Wrappers.<AddressBook>update());
    }

    /*
     * 根据id删除地址
     * */
    @Override
    public void deleteAddressBookById(Long id) {
        addressBookMapper.deleteById(id);
    }

    /*
     * 根据路径id查询地址
     * */
    @Override
    public AddressBook selectAddressBookById(Long id) {
        AddressBook addressBook = addressBookMapper.selectById(id);
        return addressBook;
    }

    /*
     * 根据传入的地址id设置默认地址
     * */
    @Override
    public void setAddressBookDefault(Long id) {
        AddressBook addressBook = addressBookMapper.selectById(id);
        addressBook.setIsDefault(AddressBook.IS_DEFAULT_YES);
    }

}
